package net.alteiar.ui.editor;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JPanel;
import javax.swing.JSpinner;

import net.alteiar.images.BasicImage;
import net.alteiar.images.IDrawable;
import net.alteiar.images.ImageFiltered;
import net.alteiar.images.filter.elements.BlockingFilter;
import net.alteiar.images.metadata.ImageInfo;
import net.alteiar.ui.MovablePanel;
import net.alteiar.ui.ZoomablePanel;
import net.alteiar.ui.editor.tools.Brush;
import net.alteiar.ui.editor.tools.Fill;
import net.alteiar.ui.editor.tools.params.MenuTool;
import net.alteiar.ui.listener.MoveListener;
import net.alteiar.ui.listener.ZoomListener;

public class ImageInfoEditorPanel extends JPanel {
	private static final long serialVersionUID = 1L;

	public enum EditorState {
		MOVE, BRUSH, FILL
	};

	private final ImageInfo info;
	private final ImageInfo infoTemp;

	private MenuTool tool;
	private final Brush toolBrush;
	private final Fill toolFill;

	private final ZoomablePanel<IDrawable> panelImage;

	private final EditorState state;
	private JSpinner spinnerSize;

	private final ImageEditorMouseListener mouseListener;

	public ImageInfoEditorPanel(BasicImage img, ImageInfo info) {
		super(new BorderLayout());
		this.info = info;
		state = EditorState.MOVE;

		infoTemp = new ImageInfo(info);
		ImageFiltered filter = buildImage(img);

		panelImage = new ZoomablePanel<IDrawable>(filter);

		MovablePanel move = new MovablePanel(panelImage);
		move.setPreferredSize(new Dimension(800, 600));

		mouseListener = new ImageEditorMouseListener(new EditorCondition(this,
				EditorState.BRUSH), panelImage);
		MoveListener moveListener = new MoveListener(new EditorCondition(this,
				EditorState.MOVE), move);
		panelImage.addMouseWheelListener(new ZoomListener(panelImage));
		panelImage.addMouseMotionListener(moveListener);
		panelImage.addMouseListener(moveListener);
		panelImage.addMouseMotionListener(mouseListener);
		panelImage.addMouseListener(mouseListener);

		toolBrush = new Brush(info, infoTemp);
		toolFill = new Fill(info, infoTemp);

		this.add(buildToolPanel(), BorderLayout.NORTH);
		this.add(move, BorderLayout.CENTER);
	}

	private ImageFiltered buildImage(BasicImage img) {
		ImageFiltered result = new ImageFiltered(img);
		// result.addImage(new GridFilter(info, 30, Color.BLACK));
		result.addImage(new BlockingFilter(info));
		result.addImage(new BlockingFilter(infoTemp));
		return result;
	}

	private JPanel buildToolPanel() {
		JPanel tools = new JPanel(new FlowLayout());
		/*
		
		MenuTool tool = new MenuTool(info, infoTemp);
		tools.add(tool);

		JButton buttonBrush = new JButton("brush");
		buttonBrush.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				toolBrush.setValue(Integer.MAX_VALUE);
				changeTool(toolBrush);
			}
		});
		tools.add(buttonBrush);

		JButton buttonFill = new JButton("fill");
		buttonFill.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				toolFill.setBorder(Integer.MAX_VALUE);
				toolFill.setValue(Integer.MAX_VALUE);
				changeTool(toolFill);
			}
		});
		tools.add(buttonFill);

		spinnerSize = new JSpinner();
		spinnerSize.addChangeListener(new ChangeListener() {
			@Override
			public void stateChanged(ChangeEvent e) {
				spinnerSizeChanged();
			}
		});
		tools.add(spinnerSize);
		spinnerSize.setValue(50);
		*/
		return tools;
	}

	private void changeTool() {
		mouseListener.setTool(tool.getTool());
	}

	private void spinnerSizeChanged() {
		switch (state) {
		case BRUSH:
			int size = (int) spinnerSize.getValue();
			toolBrush.setSize(size);
			// changeTool(toolBrush);
			break;
		default:
			break;
		}
	}

	public EditorState getState() {
		return state;
	}
}
